--
-- Created by IntelliJ IDEA.
-- User: bluda
-- Date: 2018/8/22
-- Time: 14:57
-- To change this template use File | Settings | File Templates.
-- 攻击单元，如远程攻击，魔法等
-- 这个单元会在主循环的时候，自动运行

local NORMAL_EFFECT_DISTANCE = 20

local AttackUnit = {
    speed = 200,
    trail = nil,
    track = false,
    way = "normal",
    effect = 60,
    target = nil,
    startPosition = {x=0,y=0},
    endPosition = {x=0,y=0},
    endInRight = true,
    currentPosition = {x=0,y=0},
    signed = {x=1, y=1},
    height = 0,
    createTime = 0,
    battlefield = nil,
    ui = nil,
    moveTime = 0,
    done = false,
}


-- 攻击单元创建后，必须加入到战场，才能生效
function AttackUnit:create(battlefield, createTime, way, effect, speed, track, ui, target, startPosition, endPosition, height, trail) 
    local au = xcopy(AttackUnit)
    au.speed = speed
    au.trail = trail
    au.track = track
    au.way = way
    au.effect = effect
    au.createTime = createTime
    au.target = target
    au.ui = ui
    au.height = height

    au.startPosition.x = startPosition.x
    au.startPosition.y = startPosition.y
    au.endPosition = endPosition
    au.currentPosition.x = startPosition.x
    au.currentPosition.y = startPosition.y
    au.signed.x = signed(startPosition.x, endPosition.x)
    au.signed.y = signed(startPosition.y, endPosition.y)
    au.id = math.random()
    au.battlefield = battlefield

    -- 规整话距离
    if endPosition.x >= startPosition.x then
        self.endInRight = true
    end

    if endPosition.x < startPosition.x then
        self.endInRight = false
    end

    au.done = false
    return au
end

function AttackUnit:process(dt, ct, ci)
    if self.done then
        return
    end

    local deltaDist = dt * self.speed
    
    if self.trail ~= nil then
        local deltaX = dt * self.speed
        self.currentPosition.x = self.currentPosition.x + deltaX
        if self.currentPosition.x > self.endPosition.x then
            self.currentPosition.x = self.endPosition.x
        end
        self.currentPosition.y = self:trail(self.currentPosition.x, self.startPosition, self.endPosition, self.height)
    else
        local x,y = QuadraticTrail(deltaDist, self.startPosition, self.endPosition, self.signed)
        logD(x, y, self.startPosition.x, self.startPosition.y, self.endPosition.x, self.endPosition.y)
        self.currentPosition.x = self.currentPosition.x + x
        self.currentPosition.y = self.currentPosition.y + y
        
        if self.startPosition.x <= self.endPosition.x then
            --local deltaX = dt * self.speed
           -- self.currentPosition.x = self.currentPosition.x + deltaX
            if self.currentPosition.x > self.endPosition.x then
                self.currentPosition.x = self.endPosition.x
                self.currentPosition.y = self.endPosition.y
            end
        else
            --local deltaX = dt * self.speed
            --self.currentPosition.x = self.currentPosition.x - deltaX
            if self.currentPosition.x < self.endPosition.x then
                self.currentPosition.x = self.endPosition.x
                self.currentPosition.y = self.endPosition.y
            end
        end
    end

    self.ui:setPosition(self.currentPosition)

    local dist = distance(self.currentPosition, self.endPosition)
    --logW("dist " .. dist, self.currentPosition.x, self.currentPosition.y)
    -- 如果已经抵达目标，计算效果
    if dist == 0 then
        self:calculateEffect()
    end

    -- 如果miss，超过了战场，移除
    if self.battlefield:isInBattlefieldArea(self.currentPosition) then
    else
        logW("out bf area")
        self.done = true
    end

end

-- 计算效果，计算完了，那么会消失
function AttackUnit:calculateEffect()
    -- TODO 计算效果
    local targetUnit = self.target
    if targetUnit == nil then
        return
    end

    local targetEnv = self.battlefield:getEnvEffect(targetUnit.position)
    if targetUnit:isValid() then
        targetUnit.hp = targetUnit.hp - (self.effect / (targetUnit.defence *(1 - targetEnv.dsd)) + self.effect / 2)
    end

    if targetUnit.hp < 0 then
        targetUnit.hp = 0
    end
    
    self.done = true
end

-- 攻击单元是否已经结束
function AttackUnit:isDone()
    return self.done
end


return AttackUnit